<template>

  <div
    style="width:100%;height:360px;"
    id="bar"
  ></div>

</template>

<script>
import echarts from "echarts";

export default {
  name: "dataSourceLine",
  props: {
    BarDataSource: {
      type: Array,
      default: function() {
        return [
          {
            depName: "dream",
            break_count: 0,
            fire_count: 0,
            pre_count: 0
          }
        ];
      }
    }
  },
  data() {
    return {};
  },
  watch: {
    BarDataSource(curVal, oldVal) {
      this.initEchats();
    }
  },
  mounted() {},
  methods: {
    initEchats() {
      var BarDataSource = echarts.init(document.getElementById("bar"));
      var len = this.BarDataSource.length;
      var xAxisData = [];
      var breakData = [];
      var fireData = [];
      var preData = [];
      for (let i = 0; i < len; i++) {
        let dataItem = this.BarDataSource[i];
        xAxisData.push(dataItem.depName);
        breakData.push(dataItem.break_count);
        fireData.push(dataItem.fire_count);
        preData.push(dataItem.pre_count);
      }

      var option = {
        color: ["#003399", "#3399ff", "#3366CC"],
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "shadow"
          }
        },
        legend: {
          data: ["故障", "火警", "预警"],
        },
        xAxis: [
          {
            type: "category",
            axisTick: { show: false },
            data: xAxisData
          }
        ],
        yAxis: {
          type: "value",
          interval: 1,
          axisLabel: {
            formatter: "{value}"
          },
          axisLine: {
            //---坐标轴 轴线
            show: true, //---是否显示
            //------------------- 箭头 -------------------------
            symbol: ["none", "arrow"], //---是否显示轴线箭头
            symbolSize: [8, 8], //---箭头大小
            // symbolOffset: [0, 7], //---箭头位置

            //------------------- 线 -------------------------
            lineStyle: {
              color: "#7f8081",
              width: 1,
              type: "solid"
            }
          }
        },
        series: [
          {
            name: "故障",
            type: "bar",
            barGap: 0,
            barMaxWidth: 30,
            data: breakData
          },
          {
            name: "火警",
            type: "bar",
            barMaxWidth: 30,
            data: fireData
          },
          {
            name: "预警",
            type: "bar",
            barMaxWidth: 30,
            data: preData
          }
        ]
      };
      BarDataSource.setOption(option);
      window.addEventListener("resize", function() {
        BarDataSource.resize();
      });
    }
  }
};
</script>
